package org.maxgroundstation.simulation.view;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

import org.maxgroundstation.simulation.MaxRobot;


/**
 * A frame for displaying the MAX robot simulation info.
 * @author Scott Davis
 * 
 * Copyright (C) 2010  Scott Davis
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
@SuppressWarnings("serial")
public class MaxRobotFrame extends JFrame implements Runnable {

    // Length of refresh thread sleep time (milliseconds).
    private static final long SLEEP_TIME = 30L;
    
    // Data members
    private TopDownPanel topDownPanel;
    private TurretSidePanel turretSidePanel;
    private MotorPowerPanel motorPowerPanel;
    private boolean run;
    private Thread refreshThread;
    private MaxRobot robot;
    
    /**
     * Constructor
     * @param robot the simulated MAX robot.
     */
    public MaxRobotFrame(MaxRobot robot) {
        // Use JFrame constructor.
        super("MAX Robot Simulation");
        
        this.robot = robot;
        
        setLayout(new BorderLayout(0, 0));
        
        JPanel networkPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
        add(networkPanel, BorderLayout.NORTH);
        
        JLabel networkLabel = new JLabel("IP: " + robot.getHostIP() + 
                " Port: " + robot.getPort());
        networkPanel.add(networkLabel);
        
        topDownPanel = new TopDownPanel(robot);
        add(topDownPanel, BorderLayout.CENTER);
        
        JPanel rightPanel = new JPanel(new GridLayout(2, 1));
        add(rightPanel, BorderLayout.EAST);
        
        turretSidePanel = new TurretSidePanel(robot);
        rightPanel.add(turretSidePanel);
        
        motorPowerPanel = new MotorPowerPanel(robot);
        rightPanel.add(motorPowerPanel);
        
        pack();
        setVisible(true);
        
        // Add window listener to exit application.
        addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                shutdown();
            }
        });
        
        // Start refresh thread.
        start();
    }
    
    /**
     * Sets the refresh thread to run or not.
     * @param run true if panel is to be refreshed.
     */
    public void setRun(boolean run) {
        this.run = run;
    }
    
    /**
     * Starts the update thread.
     */
    private void start() {
        if ((refreshThread == null) || !run) {
            refreshThread = new Thread(this, "Frame refresh thread");
            refreshThread.start();
        }
    }
    
    /**
     * Run the update thread.
     */
    public void run() {
    	run = true;
        while (run) {
            try {
                Thread.sleep(SLEEP_TIME);
            } 
            catch (InterruptedException e) {}
            
            // Repaint the frame.
            repaint();
        }
    }
    
    /**
     * Shutdown the frame and robot sim.
     */
    private void shutdown() {
        setRun(false);
        robot.destroy();
        System.exit(0);
    }
    
    /**
     * Main method for running app.
     * @param args arguments.
     */
    public static void main(String[] args) {
        MaxRobot robot = new MaxRobot();
        System.out.println("MAX robot created successfully");
        new MaxRobotFrame(robot);
        System.out.println("MAX robot UI created successfully");
    }
}